package com.ruoyi.web.controller.ccc3gc;

import com.ccc3gc.mysystem.bean.UserAddress;
import com.ccc3gc.mysystem.service.UserAddressService;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/userAddress")
public class UserAddressController {
    @Autowired
    public UserAddressService userAddressService;

    //获取用户地址列表
    @GetMapping("/list/{userId}")
    public AjaxResult queryUserAddress(@PathVariable int userId){
        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userId);
        List<UserAddress> userAddresses = userAddressService.selectAddressByUserId(userAddress);
        return AjaxResult.success(userAddresses);
    }

    //获取用户默认地址
    @GetMapping("/userDefAdd/{userId}")
    public AjaxResult userAddDef(@PathVariable int userId){
        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userId);
        userAddress.setDefaultUser(1);
        UserAddress userAddresses = userAddressService.selectDetAdd(userAddress);
        return AjaxResult.success(userAddresses);
    }

    //新增用户地址
    @PostMapping("/addUserAddress")
    public AjaxResult addUserAddress(@RequestBody UserAddress userAddress){
        int rows = userAddressService.insertUserAddress(userAddress);
        if(rows>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //修改地址信息
    @PostMapping("/updateUserAddress/{addressId}")
    public AjaxResult updateUserAddress(@RequestBody UserAddress userAddress,@PathVariable int addressId){
        int rows = userAddressService.updateUserAddress(userAddress, addressId);
        if (rows>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
    //删除用户地址
    @GetMapping("/deleteUserAddress")
    public AjaxResult deleteUserAddressByUserId(@RequestParam("userId") int userId,@RequestParam("addressId") int addressId){
        int rows = userAddressService.deleteUserAddressByUserId(userId, addressId);
        if(rows>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
